package com.houxm.caipiao.algorithm.red.impl;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.houxm.caipiao.algorithm.Forecast;
import com.houxm.caipiao.model.Ssq_drawing_history;

/**
 * <p>(1)准备好上期开奖号码。</p>
<p>(2)计算出上期开奖号码的和值。</p>
<p>(3)用计算好的和值分别减去每一个奖号，得到的差再除以每一个奖号。</p>
<p>(4)最后取每个奖号被除后的商。保留一位小数和一位整数。取两位尾数。</p>
<p>(5)杀号，把握好认为绝不会出号杀去。</p>
<p>范例:&nbsp;</p>
<p>比如求2014年的005期的开奖号码。</p>
<p>1、那么就先找出004期的开奖号码。004期的奖号红球是:01 04 19 22 24 25篮球15</p>
<p>2、计算和值。004期和值是95。</p>
<p>3、用95分别减每个奖号，再除以每个奖号。</p>
<p>(95-&nbsp; 1)/&nbsp; 1=95</p>
<p>(95-&nbsp; 4)/&nbsp; 4=22.7</p>
<p>(95-19)/19=4</p>
<p>(95-22)/22=3.3</p>
<p>(95-24)/24=2.9</p>
<p>(95-25)/25=2.8</p>
<p>4、取其结果的尾数。</p>
<p>一、95没有小数，取一个95尾</p>
<p>二、22.7取个位小数7和十位2，取27尾</p>
<p>三、4整数一个。就取4尾</p>
<p>四、3.3取3尾</p>
<p>五、2.9取2.9</p>
<p>六、2.8取2.8</p>
<p>七、杀号。杀4V</p>
<p>最后综合取尾号，去掉重复尾号。(去掉里面的重复2V9V留一个)，(杀4V不用)(95V)，9 19 29 5 15 25，(27V)，2 12 22 32 7 17 27(3.3V)，用3V，3 13 23 33(28V)去掉重复2V取8尾，8 18 28</p>
<p>共选号码十九枚是02 03 05 07 08 09 12 13 15 17 18 19 22 23 25 27 28 32 33</p>
<p>005期的开奖号码是15 18 23 27 32 33刚好围中6红。</p>
<p>以上就是彩宝贝小编为大家整理的双色球必中六红的算法公式，希望大家用心体会，也助大家早中大奖。</p>
 * @author hxm
 *
 */
public class TailExtendForcast implements Forecast {
	
	private static Map<Integer, Integer[]> map = new HashMap<Integer, Integer[]>();
	
	static {
		map.put(0, new Integer[]{10, 20, 30});
		map.put(1, new Integer[]{1, 11, 21, 31});
		map.put(2, new Integer[]{2, 12, 22, 32});
		map.put(3, new Integer[]{3, 13, 23, 33});
		map.put(4, new Integer[]{4, 14, 24});
		map.put(5, new Integer[]{5, 15, 25});
		map.put(6, new Integer[]{6, 16, 26});
		map.put(7, new Integer[]{7, 17, 27});
		map.put(8, new Integer[]{8, 18, 28});
		map.put(9, new Integer[]{9, 19, 29});
	}

	@Override
	public List<Integer> forecast(int term) {
		List<Integer> result = new ArrayList<Integer>();
		//最新TERM_TIMES期开奖号码，倒序的TERM_TIMES期次开出的红球号码
		String sql = "select * from ssq_drawing_history where term<"+term+" order by term desc limit 1";
		Ssq_drawing_history lottery = Ssq_drawing_history.me.findFirst(sql);
//		int currentTerm = lottery.getItem(0);
		int redSum = 0;
		Set<Integer> tail = new HashSet<Integer>();
		for(int j=1;j<7;j++){
			int red = lottery.getItem(j);
			redSum += red;
		}
		for(int j=1;j<7;j++){
			int red = lottery.getItem(j);
			if((redSum-red)%red==0) {
				//整除
				int num = (redSum-red)/red;
				if(num<10) {
					tail.add(num);
				}else if(num>=10 && num<100) {
					tail.add(num%10);
					tail.add(num/10);
				}else {
//					123/10=12  123%10=3  123%100=23 23/10=2
					tail.add(num%10);
					tail.add(num/100);
					int tmp = num%100;
					tail.add(tmp/10);
				}
			}else {
				double num = (redSum-red)*1.0d/red;
				String tmp = formatDouble(num);
				int num1 = Integer.parseInt(tmp.split(".")[0]);
				int num2 = Integer.parseInt(tmp.split(".")[1]);
				if(num<10) {
					tail.add(num1);
				}else {
					tail.add(num1%10);
					tail.add(num1/10);
				}
				tail.add(num2);
			}
		}
		for(int i : tail) {
			result.addAll(Arrays.asList(map.get(i)));
		}
		
		return result;
	}
	
	public static String formatDouble(double d) {
		BigDecimal bg = new BigDecimal(d).setScale(1, RoundingMode.DOWN);
        return String.valueOf(bg.doubleValue());
    }

	public static void main(String[] args) {
		System.out.println(formatDouble(34.567));
	}
}
